package com.cgc.venus.server.core.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by xyr on 2017/3/20 0020.
 * 日期处理类
 */
public class DateUtil {

    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";

    public static String getUTCTimeStr() {
        SimpleDateFormat format = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
        StringBuffer UTCTimeBuffer = new StringBuffer();
        // 1、取得本地时间：
        Calendar cal = Calendar.getInstance() ;
        // 2、取得时间偏移量：
        int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
        // 3、取得夏令时差：
        int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
        // 4、从本地时间里扣除这些差量，即可以取得UTC时间：
        cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day) ;
        UTCTimeBuffer.append(" ").append(hour).append(":").append(minute).append(":").append(second) ;
        try{
            format.parse(UTCTimeBuffer.toString()) ;
            return UTCTimeBuffer.toString() ;
        }catch(ParseException e)
        {
            e.printStackTrace() ;
        }
        return null ;
    }


    /*
    * timestampString:数据库dateTime类型 1485972629000
    * formats 时间表达式:yyyy-MM-dd HH:mm:ss
    * 将long 转 2017-02-02 02:10:29
    * */
    public static String TimeStamp2Date(Timestamp timestamp, String formats){
        //Long timestamp = Long.parseLong(timestampString);
        String date = new SimpleDateFormat(formats).format(timestamp);
        return date;
    }

    /*
    * 将2017-02-02 02:10:29 转 long
    * */
    public static long parseStr2Date(String timeStr,String formats){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formats);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1.getTime();
    }

    /*
    * 比较时间差,秒
    * */
    public static int countTimeDifference(long startTime,long endTime){
        return  (int)((endTime - startTime) / 1000);
    }

    /*
    * 得到当前的时间
    * */
    public static String getCurrentTime(String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    // 时间相加
    public static String calculate(String pattern,String time,long amount) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date date = format.parse(time);
            long dateLong = date.getTime() + amount;
            time = format.format(dateLong);
        } catch (ParseException e) {
            return time;
        }
        return time;
    }

    /*
    * 2017-11-21 计算应用创建的时间差
    * */
    public static String getTimeDifference(String pattern,String fromDate, String toDate) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat(pattern);
        long from = 0;
        long to = 0;
        try {
            from = simpleFormat.parse(fromDate).getTime();
            to = simpleFormat.parse(toDate).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 得到秒差
        int timeDiff = (int) ((to - from)/(1000));

        if (timeDiff < 0) {
            timeDiff = timeDiff * -1;
        }

        String time = "";

        if (timeDiff >= 60 && timeDiff < 3600) {
            // 返回分钟差
            timeDiff = (int) ((timeDiff)/(60));
            time = timeDiff +"分钟前";
        } else if (timeDiff >= 3600 && timeDiff < 86400) {
            timeDiff = (int) ((timeDiff)/( 60 * 60));
            time = timeDiff +"小时前";
        } else if (timeDiff >= 86400) {
            timeDiff = (int) ((timeDiff)/(60 * 60 * 24));
            time = timeDiff +"天前";
        } else {
            time = timeDiff + "秒前";
        }
        return time;

    }


    /*
    * 2017-3-29 计算应用创建的时间差
    * */
    public static String getTimeDifference2(String fromTime) {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        long from = 0;
        long to = 0;
        try {
            from = simpleFormat.parse(fromTime).getTime();
            to = new Date().getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 得到秒差
        int timeDiff = (int) ((to - from)/(1000));

        if (timeDiff < 0) {
            timeDiff = timeDiff * -1;
        }

        String time = "";

        if (timeDiff >= 60 && timeDiff < 3600) {
            // 返回分钟差
            timeDiff = (int) ((timeDiff)/(60));
            time = timeDiff +"分钟前";
        } else if (timeDiff >= 3600 && timeDiff < 86400) {
            timeDiff = (int) ((timeDiff)/( 60 * 60));
            time = timeDiff +"小时前";
        } else if (timeDiff >= 86400) {
            timeDiff = (int) ((timeDiff)/(60 * 60 * 24));
            time = timeDiff +"天前";
        } else {
            time = timeDiff + "秒前";
        }
        return time;

    }



    /**
     * 根据时间关键字解析成 一对时间戳  用于日志查询
     * @param times
     * @return
     * @throws Exception
     */
    public static long[] handleTimes1(String times) throws Exception{
        if(times==null){
            return null;
        }
        if("All".equals(times)){
            return null;
        }
        if(times.equals("")){
            return null;
        }
        long start=0;
        long end=0;
        if("Today".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        } else if("This_week".equals(times)){
            Calendar cal = Calendar.getInstance();
            int week=cal.get(Calendar.DAY_OF_WEEK);//得到是星期几
            cal.add(Calendar.DAY_OF_MONTH, 2-week);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("This_month".equals(times)){
            Calendar cal = Calendar.getInstance();
            int day=cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1-day);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("This_Year".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, cal.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Yesterday".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);

            end=cal.getTimeInMillis();
        }else if("This_month".equals(times)){
            Calendar cal = Calendar.getInstance();
            int year=cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1-year);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("The_day_before_yesterday".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -2);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -2);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("Last_week_of_the_day".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("The_previous_week".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -7);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("The_previous_month".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.MONTH, -1);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("The_previous_year".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, -1);
            cal.set(Calendar.MONTH, 0);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, -1);
            cal.set(Calendar.MONTH, 11);
            cal.set(Calendar.DAY_OF_MONTH, 31);
            cal.set(Calendar.HOUR_OF_DAY, 23);//小时
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 999);
            end=cal.getTimeInMillis();

        }else if("Last_15_minutes".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MINUTE, -15);//分钟
            start=cal.getTimeInMillis();
            end=System.currentTimeMillis();

        }else if("Last_30_minutes".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.MINUTE, -30);//分钟
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_1_hours".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -1);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_4_hours".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -4);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_12_hours".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -12);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_24_hours".equals(times)){
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.HOUR_OF_DAY, -24);//小时
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_7_day".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -6);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_30_day".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -29);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_60_day".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -59);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_90_day".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -89);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_6_month".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -179);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_1_year".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -364);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_2_year".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -729);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if("Last_5_year".equals(times)){
            //含今天
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_MONTH, -1824);
            cal.set(Calendar.HOUR_OF_DAY, 0);//小时
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            start=cal.getTimeInMillis();

            end=System.currentTimeMillis();
        }else if(times.split("~")!=null){
            String[] tmp=times.split("~");
            if(tmp.length>0){
                String ts=tmp[0];
                String te=tmp[1];
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                Date d1=sdf.parse(ts);
                Date d2=sdf.parse(te);
                start=d1.getTime();
                end=d2.getTime();
            }else{
                return null;
            }
        }else {
            System.out.println("=============没有匹配任何时间=============");
            return null;
        }
        long[] l=new long[2];
        l[0]=start;
        l[1]=end;
        return l;
    }

    /**
     * 根据时间关键字解析成 一对时间戳  用于监控
     * @param times
     * @return
     * @throws Exception
     */
    public static String[] handleTimes2(String times){
        String[] strs = new String[2];
        String[] strTmp=times.split(",");
        String format="yyyy-MM-dd'T'HH:mm:00'Z'";
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        if(strTmp.length==1){
            Calendar endTime = Calendar.getInstance();
            endTime.add(Calendar.HOUR,-8);
            String time1=sdf.format(endTime.getTime());
            Calendar startTime = Calendar.getInstance();
            startTime.add(Calendar.HOUR,-8);
            if("5min".equals(strTmp[0])){
                 startTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
                 String time0=sdf.format(startTime.getTime());
                 strs[0]=time0;
                 strs[1]=time1;
                return strs;
             }if("30min".equals(strTmp[0])){
                startTime.add(Calendar.MINUTE, -30);// 30分钟之前的时间
                String time0=sdf.format(startTime.getTime());
                strs[0]=time0;
                strs[1]=time1;
                return strs;
            }if("1h".equals(strTmp[0])){
                startTime.add(Calendar.HOUR, -1);// 1h之前的时间
                String time0=sdf.format(startTime.getTime());
                strs[0]=time0;
                strs[1]=time1;
                return strs;
            }if("6h".equals(strTmp[0])){
                startTime.add(Calendar.HOUR, -6);// 1h之前的时间
                String time0=sdf.format(startTime.getTime());
                strs[0]=time0;
                strs[1]=time1;
                return strs;
            }if("1d".equals(strTmp[0])){
                startTime.add(Calendar.DAY_OF_MONTH, -1);// 1h之前的时间
                String time0=sdf.format(startTime.getTime());
                strs[0]=time0;
                strs[1]=time1;
                return strs;
            }
            if("1week".equals(strTmp[0])){
                startTime.add(Calendar.DAY_OF_MONTH, -7);// 1h之前的时间
                String time0=sdf.format(startTime.getTime());
                strs[0]=time0;
                strs[1]=time1;
                return strs;
            }

        }
        return strs;
    }

    /**
     *
     * @param times
     * @return
     */
    public static String handleTimes3(String times,String timestamp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        SimpleDateFormat sdf1=new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdf2=new SimpleDateFormat("MM/dd HH:mm");
        String result=null;
        try {
            Date date=sdf.parse(timestamp);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR,+8);
            String[] strTmp=times.split(",");
            if(strTmp.length==1){
               String str=strTmp[0];
               if("5min".equals(str)||"30min".equals(str)||"1h".equals(str)){
                   result =sdf1.format(calendar.getTime());
                   return result;
               }
               if("6h".equals(str)||"1d".equals(str)||"1week".equals(str)){
                   result =sdf2.format(calendar.getTime());
                   return result;
               }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 日期格式转换yyyy-MM-dd HH:mm:ss to  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
     *
     * @throws ParseException
     */
    public static String dealDateFormat2(String oldDateStr) throws ParseException {
        //此格式只有  jdk 1.7才支持  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
        //这个后面的.SSSXXX写了的话这一行就直接抛异常了，所以我去掉了，还有前面的T  一定要用英文的单引号包裹起来
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date  date = df.parse(oldDateStr);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//date 换成已经已知的Date对象
        cal.add(Calendar.HOUR_OF_DAY, -8);// before 8 hour
        SimpleDateFormat df1 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX");
        TimeZone timeZone=df1.getTimeZone();
        String r=df1.format(cal.getTime());
        return r;
    }

    /**
     * 日期格式转换EEE MMM dd yyyy hh:mm:ss to  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
     *
     * @throws ParseException
     */
    public static String dealDateFormat0(String oldDateStr) throws ParseException {
        //此格式只有  jdk 1.7才支持  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
        //这个后面的.SSSXXX写了的话这一行就直接抛异常了，所以我去掉了，还有前面的T  一定要用英文的单引号包裹起来
        DateFormat df = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss ",Locale.ENGLISH);
        Date  date = df.parse(oldDateStr);
        SimpleDateFormat df1 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX");
        return df1.format(date);
    }

    /**
     * 日期格式转换EEE MMM dd yyyy hh:mm:ss z to  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
     *
     * @throws ParseException
     */
    public static String dealDateFormat(String oldDateStr) throws ParseException {
        //此格式只有  jdk 1.7才支持  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
        //这个后面的.SSSXXX写了的话这一行就直接抛异常了，所以我去掉了，还有前面的T  一定要用英文的单引号包裹起来
        DateFormat df = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z",Locale.ENGLISH);
        Date  date = df.parse(oldDateStr);
        SimpleDateFormat df1 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX");
        return df1.format(date);
    }

    /**
     * 日期格式转换EEE MMM dd yyyy hh:mm:ss z to  yyyy-MM-dd HH:mm:ss
     *
     * @throws ParseException
     */
    public static String dealDateFormat1(String oldDateStr) throws ParseException {
        //此格式只有  jdk 1.7才支持  yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX
        //这个后面的.SSSXXX写了的话这一行就直接抛异常了，所以我去掉了，还有前面的T  一定要用英文的单引号包裹起来
        DateFormat df = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z",Locale.ENGLISH);
        Date  date = df.parse(oldDateStr);
        SimpleDateFormat df1 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        return df1.format(date);
    }

    public static String zoneDateTimeStrConvertToShangHaiTimeStr(String patten,String zoneDateTimeStr){
        //String str="2018-01-25T08:09:19.758336841Z";
        //2018-05-21T17:25:07.481+08:00[Asia/Shanghai]
        ZonedDateTime atZone2=ZonedDateTime.parse(zoneDateTimeStr);
        ZoneId zone=ZoneId.of("Asia/Shanghai");
        ZonedDateTime atZone3=ZonedDateTime.of(atZone2.toLocalDateTime(), zone);
        LocalDateTime localDateTime=null;
        if(zoneDateTimeStr.indexOf("Z")!=-1){
            localDateTime=atZone3.toLocalDateTime().plusHours(8);
        }else{
            localDateTime=atZone3.toLocalDateTime();
        }
        DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern(patten);
        return localDateTime.format(dateTimeFormatter);
    }

}
